import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { EventProvider } from '../../providers/event/event';
/**
 * Generated class for the Userinfo page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-userinfo',
	templateUrl: 'userinfo.html',
})
export class UserinfoPage {
	public userInfo = {
		name: '',
		email: '',
		phone: '',
		mobile: '',
		remarks: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public event: EventProvider) {
		
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad Userinfo');
		this.loadData();
	}

	/*加载数据*/
	loadData() {
		this.storage.getStorage("user").then(val => {
			//请求方式如下
			let params = {
				user_id: val['user_id']
			};
			this.http.postJson('user', 'douserinfo.do', params).then(res => {
				if (res['success'] == true) {
					let info = res['data'];
					//请求成功
					this.userInfo.name = info['name'];
					this.userInfo.email = info['email'];
					this.userInfo.phone = info['phone'];
					this.userInfo.mobile = info['mobile'];
					this.userInfo.remarks = info['remarks'];
				} else {
					this.dialog.showToast("top", res['msg']);
				}
			});
		});
	}
	/*保存数据*/
	saveOnSuccess() {
		if (this.saveCheck()) {
			this.storage.getStorage("user").then(val => {
				//请求方式如下
				let params = {
					user_id: val['user_id'],
					name: this.userInfo.name,
					email: this.userInfo.email,
					phone: this.userInfo.phone,
					mobile: this.userInfo.mobile,
					remarks: this.userInfo.remarks
				};
				this.http.postJson('user', 'dosaveuser.do', params).then(res => {
					if (res['success'] == true) {
						//关闭modal
						this.navCtrl.pop();
						//刷新当前数据
						this.event.sendEvents(this.event.EVENT_CENTER, {});
						//修改成功
						this.dialog.showToast("top", "资料修改成功");
					} else {
						this.dialog.showToast("top", res['msg']);
					}
				});
			});
		}
	}

	saveCheck() {
		if (this.userInfo.name == "") {
			this.dialog.showAlert("提示", "请输入姓名");
			return false;
		}
		if (this.userInfo.mobile == "") {
			this.dialog.showAlert("提示", "请输入手机号");
			return false;
		}
		return true;
	}
}